# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: server.py
Author: kij
Create Date: 2021/3/23
-------------------------------------------------
"""
import socket
import send
from multiprocessing import Process
from concurrent.futures import ThreadPoolExecutor

def client(conn):
	try:
		while True:
			res = send.recv_data(conn)
			# 客户端不接收数据后断开
			if not res:
				break
			send_str = "res===%s" % res.decode('utf-8')
			send.send_data(conn, send_str)
	except ConnectionResetError as e:
		print(".", e)
		conn.close()


def server():
	server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	server.bind(send.IP_POST)
	server.listen(5)

	while True:
		conn, addr = server.accept()
		print(conn)
		print(addr)
		# p = Process(target=client, args=(conn,))
		# p.start()
		pool.submit(client, conn)
	server.close()

if __name__ == '__main__':
	pool = ThreadPoolExecutor(2)
	server()
